<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
</head>
<body>
<form action="#" method="post" id="frmInfo">
    <div id="err" style="color:red">

    </div>
    <div>
        姓名:<input id="name" name="name"/>
    </div>
    <div>
        身份证:<input id="idno" name="idno"/>
    </div>
    <div>
        <input type="submit" />
    </div>
</form>
<script type="text/javascript">
    document.getElementById("frmInfo").onsubmit = function(){
        //在JS中定义正则表达式对象只需要在 /正则表达式/
        var regex1 = /^[\u4e00-\u9fa5]{2,8}$/;
        var regex2 = /^[1234568]\d{16}[0-9xX]$/
        var name = document.getElementById("name").value;
        var idno = document.getElementById("idno").value;
        if(regex1.test(name) == false){//利用正则校验数据,true代表匹配,false代表不匹配
            document.getElementById("err").innerHTML = "无效姓名";
            return false;
        }else if(regex2.test(idno) == false){
            document.getElementById("err").innerHTML = "无效身份证号";
            return false;
        }else{
            alert("验证通过,准备提交")
            return true;
        }

    }
</script>
</body>
</html>